package com.fufufu.mail.ware.service.impl;

import com.fufufu.mail.ware.entity.PurchaseDetailEntity;
import com.fufufu.mail.ware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.Query;

import com.fufufu.mail.ware.dao.WareSkuDao;
import com.fufufu.mail.ware.entity.WareSkuEntity;
import com.fufufu.mail.ware.service.WareSkuService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    private PurchaseDetailService purchaseDetailService;

    @Override
    public void saveBypurchaseDetais(List<PurchaseDetailEntity> purchaseDetailEntities) {
        if (purchaseDetailEntities == null || purchaseDetailEntities.size() == 0) {
            return;
        }
        // 先根据id查出具体的采购细节
        List<Long> detailIds = purchaseDetailEntities.stream().map(PurchaseDetailEntity::getId).collect(Collectors.toList());
        purchaseDetailEntities = purchaseDetailService.listByIds(detailIds);
        // 获得其skuid，用set防止重复,可以知道有哪些sku的库存更新了
        Set<Long> skuIdSet = purchaseDetailEntities.stream().map(PurchaseDetailEntity::getSkuId).collect(Collectors.toSet());
        if (skuIdSet.size() == 0) {
            return;
        }
        // 通过skuid查询已经存在的商品库存
        List<WareSkuEntity> needUpdate = this.list(new QueryWrapper<WareSkuEntity>().in("sku_id", skuIdSet));
        Map<Long, WareSkuEntity> skuIdWareSkuEntityMapping = new HashMap<>();
        for (WareSkuEntity wareSkuEntity : needUpdate) {
            skuIdWareSkuEntityMapping.put(wareSkuEntity.getSkuId(), wareSkuEntity);
        }
        List<WareSkuEntity> needSave = new ArrayList<>();
        for (PurchaseDetailEntity purchaseDetailEntity : purchaseDetailEntities) {
            WareSkuEntity wareSkuEntity = skuIdWareSkuEntityMapping.get(purchaseDetailEntity.getSkuId());
            if (wareSkuEntity != null) { // 有就直接加
                wareSkuEntity.setStock(wareSkuEntity.getStock() + purchaseDetailEntity.getSkuNum());
            } else { // 没有就新建
                wareSkuEntity = new WareSkuEntity();
                wareSkuEntity.setSkuId(purchaseDetailEntity.getSkuId());
                wareSkuEntity.setWareId(purchaseDetailEntity.getWareId());
                wareSkuEntity.setStock(purchaseDetailEntity.getSkuNum());
                needSave.add(wareSkuEntity);
            }
        }
        this.saveBatch(needSave);
        this.updateBatchById(needUpdate);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StringUtils.hasText(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (StringUtils.hasText(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

}